package com.itheima.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/life.html")
public class LifeFilter implements Filter {
    //tomcat一启动就使用反射调用该无参构造器创建Filter对象
    public LifeFilter() {
        System.out.println("我是Filter的无参构造器....");
    }

    public void destroy() {
        //tomcat关闭时调用该方法
        System.out.println("destroy....");
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        chain.doFilter(request, response);
    }

    public void init(FilterConfig config) throws ServletException {
        //在使用反射创建对象后就调用该初始化方法
        System.out.println("init.....");

    }

}